#include "commonpage.h"
#include<QDebug>
#include "ui_commonpage.h"
#include"listitembox.h"
#include<QListWidgetItem>
#include"music.h"
#include"mymusic.h"
CommonPage::CommonPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CommonPage)
{
    ui->setupUi(this);
    ui->pageMusicList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    connect(ui->playAllbutton,&QPushButton::clicked,[=](){
        //点击播放全部按钮之后发射信号，通知MYMusic播放对应pagetype页面包含的所有歌曲
        emit playAll(pageType);
    });
    connect(ui->pageMusicList,&QListWidget::doubleClicked,this,[=](const QModelIndex &index){
        //双击后，发射信号告诉MYMusic处理播放
       emit playMusicByIndex(this,index.row());
    });
}

CommonPage::~CommonPage()
{
    delete ui;
}

void CommonPage::setCommonPageUI(const QString &text, const QString &imagePath)
{
   ui->musicImageLabel->setPixmap(QPixmap(imagePath));
   ui->musicImageLabel->setScaledContents(true);
   ui->pageTitle->setText(text);
}

void CommonPage::setMusicListType(PageType pageType)
{
    this->pageType=pageType;
}

void CommonPage::addMusicToMusicPage(MusicList &musicList)
{
    musicOfPage.clear();
   for(auto e:musicList)
   {
       switch(pageType)
       {
         case LIKE_PAGE:
           if(e.getIsLike())
           {
               musicOfPage.push_back(e.getMusicUuid());
           }
            break;
         case LOCAL_PAGE:
               musicOfPage.push_back(e.getMusicUuid());
            break;
         case RECENT_PAGE:
           if(e.getIsHistory())
           {
               musicOfPage.push_back(e.getMusicUuid());
           }
            break;
         default:
           qDebug()<<"暂未支持";
       }
   }
}

void CommonPage::refresh(MusicList &musicList)
{
    ui->pageMusicList->clear();
   addMusicToMusicPage(musicList);
   for(auto musicId: musicOfPage)
   {
       auto it=musicList.findMusicByMusicId(musicId);
       if(it==musicList.end())
           continue;

   ListItemBox*listitembox=new ListItemBox(this);
   listitembox->setMusicName(it->getMusicName());
   listitembox->setMusicSinger(it->getMusicSinger());
   listitembox->setMusicAlbum(it->getMusicAlbum());
   listitembox->setLikeButton(it->getIsLike());
   QListWidgetItem*item=new QListWidgetItem(ui->pageMusicList);
   item->setSizeHint(QSize(listitembox->width(),listitembox->height()));
   ui->pageMusicList->setItemWidget(item,listitembox);
   connect(listitembox,&ListItemBox::setIsLike,this,[=](bool isLike){
       //更新歌曲状态
          emit updateLikeMusic(isLike,it->getMusicUuid());
   });
   //触发窗口重绘
   repaint();
   }
}

void CommonPage::addMusicToPlayerlist(MusicList &musicList, QMediaPlaylist* playerlist)
{
   for(auto music:musicList)
   {
       switch(pageType)
       {
         case LIKE_PAGE:
           if(music.getIsLike())
           {
               playerlist->addMedia(music.getMusicPath());
           }
           break;
       case LOCAL_PAGE:
           playerlist->addMedia(music.getMusicPath());
           break;
       case RECENT_PAGE:
           if(music.getIsHistory())
           {
               playerlist->addMedia(music.getMusicPath());
           }
           break;
       default:
           qDebug()<<"未知";
       }
   }
}

QString CommonPage::getMusicidOfIndex(int index)
{
    if(index>=musicOfPage.size())
    {
        qDebug()<<"无法找到";
        return "";
    }
    return musicOfPage[index];
}

void CommonPage::setMusicImage(QPixmap pixmap)
{
    ui->musicImageLabel->setPixmap(pixmap);
    ui->musicImageLabel->setScaledContents(true);
}
